//
//  SupplyDemandAddViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/3/23.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "SupplyDemandAddViewController.h"
#import "PLTextView.h"
#import "ImageSelectEditorView.h"
#import "NavigationBarToggleTabView.h"

@interface SupplyDemandAddViewController ()<UIScrollViewDelegate>{
    UIScrollView *scrollViewBG;
    PLTextView *txtContent;
    ImageSelectEditorView *imageEditorV;
    
    float ft_hieght;
    NSString *strType;
}

@end

@implementation SupplyDemandAddViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
    //注册监听键盘事件
    RegisterNotify(UIKeyboardWillShowNotification, @selector(handleKeyboardDidShow:))
    RegisterNotify(UIKeyboardWillHideNotification, @selector(handleKeyboardWillHide:))
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    RemoveNofify
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self navbarTitle:@""];
    [self initNavTopButtons];
    
    
    scrollViewBG=[[UIScrollView alloc] initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, kContentHeight-40)];
    [self.view addSubview:scrollViewBG];
    scrollViewBG.delegate=self;
    UIView *viewBG=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 140)];
    viewBG.backgroundColor=[UIColor whiteColor];
    [scrollViewBG addSubview:viewBG];
    [scrollViewBG setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    
    txtContent=[[PLTextView alloc]initWithFrame:CGRectMake(10,10, kScreenWidth-20, 120)];
//    txtContent.PlaceholderColor=rgbTitleColor;
    txtContent.Placeholder=@"描述一下您的想法...";
    txtContent.font=fontTitle;
    [scrollViewBG addSubview:txtContent];
    
    UIButton *btnSend=[RHMethods buttonWithFrame:CGRectMake(0, YH(scrollViewBG), kScreenWidth, 40) title:@"确定发布" image:nil bgimage:nil];
    [btnSend setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnSend.backgroundColor=rgbpublicColor;
    [btnSend addTarget:self action:@selector(sendButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnSend];
    [btnSend setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
    
    
    [self initImagesView];
}

-(void)initNavTopButtons{
    NavigationBarToggleTabView *toggleTab=[[NavigationBarToggleTabView alloc] initWithFrame:CGRectMake((kScreenWidth-150)/2, 28, 150, 28)];
    [self.navView addSubview:toggleTab];
    [toggleTab setTiltelArray:@[@{@"title":@"我要牌照",@"id":@"1"},@{@"title":@"我有牌照",@"id":@"2"}] toggleTab:^(NSDictionary *dicSelect) {
        strType=[dicSelect valueForJSONStrKey:@"id"];
        if ([[dicSelect valueForJSONStrKey:@"id"] isEqualToString:@"1"]) {
            
        }else{
            
        }
    } selectIndex:[self.userInfo isEqualToString:@"1"]?0:1];
}

-(void)initImagesView{
    imageEditorV=[[ImageSelectEditorView alloc] initWithFrame:CGRectMake(0, YH(txtContent), kScreenWidth, 10)];
    [scrollViewBG addSubview:imageEditorV];
    imageEditorV.maxNumber=9;
    __weak SupplyDemandAddViewController *viewC=self;
    [imageEditorV setImageUrlArray:@[] imageChange:^(NSInteger suc,CGFloat h) {
        ft_hieght=YH(imageEditorV);
        [viewC refreshContentSize];
    }];
    
}
#pragma mark button
-(void)sendButtonClicked{
    NSString *strContent=txtContent.text;
//    if (![strContent notEmptyOrNull]) {
//        [SVProgressHUD showImage:nil status:@"请输入"];
//        return;
//    }
    krequestParam
    [dictparam setValue:strType forKey:@"type"];
    [dictparam setValue:strContent forKey:@"content"];
    NSArray *arrayImages=[imageEditorV getRequsetArrywithkey:@"path" removeServer:NO];
    [NetEngine uploadAllFileAction:XKFV3demanddemandadd withParams:dictparam fileArray:arrayImages onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            [self backRefreshData];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [super backButtonClicked:nil];
            });
        }else{
            [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
        }
    } onError:^(NSError *error) {
        [SVProgressHUD showImage:nil status:alertErrorTxt];
    } withMask:SVProgressHUDMaskTypeBlack];
    
}
#pragma mark UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView==scrollViewBG) {
        [self.view endEditing:YES];
    }
}
#pragma mark Keyboard
-(void)handleKeyboardDidShow:(NSNotification *)notification{
    
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
    CGFloat distanceToMove = kbSize.height;
    NSLog(@"---->动态键盘高度:%f",distanceToMove);
  
    [UIView animateWithDuration:0.3 animations:^{
        scrollViewBG.frameHeight=kScreenHeight-kTopHeight-distanceToMove;
    } completion:^(BOOL finished) {
        
        [self refreshContentSize];
    }];
}
- (void)handleKeyboardWillHide:(NSNotification *)notification{
    [UIView animateWithDuration:0.3 animations:^{
        scrollViewBG.frameHeight=kScreenHeight-kTopHeight-40;
    }completion:^(BOOL finished) {
        [self refreshContentSize];
    }];
}
-(void)refreshContentSize{
    scrollViewBG.contentSize=CGSizeMake(kScreenWidth-1, ft_hieght>H(scrollViewBG)+1?ft_hieght:H(scrollViewBG)+1);
}


-(void)backRefreshData{
    for (id controller in self.navigationController.viewControllers) {
        if ([NSStringFromClass([controller class]) isEqualToString:@"SupplyDemandViewController"]) {
            if ([(NSObject*)controller respondsToSelector:@selector(popRefreshData)]) {
                [controller performSelector:@selector(popRefreshData) withObject:nil afterDelay:0.01];
            }
            continue;
        }
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
